﻿using Approval.Infrastructure;
using Approval.Infrastructure.Impl;
using Approval.Infrastructure.Interface;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using System.Text;

namespace Approval.ReadAPI.Extensions
{
    /// <summary>
    /// 扩展方法
    /// </summary>
    public static class ServiceIConntions
    {
        /// <summary>
        /// 注入仓储
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public static IServiceCollection AddService(this IServiceCollection services)
        {

            services.AddScoped(typeof(IBaseRepository<>), typeof(BaseRepsitory<>));
            services.AddScoped<IRLZRelationRepository, RLZRelationRepository>();
            services.AddScoped<IHCTRelationRepository, HCTRelationRepository>();
            services.AddScoped<IPayRelationRepository, PayRelationRepository>();
            services.AddScoped<IPurchaseRelationRepository, PurchaseRelationRepository>();
            services.AddScoped<IAcceptanceRelationRepository, AcceptanceRelationRepository>();
            return services;

        }




        /// <summary>
        /// 注册JWT
        /// </summary>
        /// <param name="collection"></param>
        /// <param name="builder"></param>

        public static void AddJWT(this IServiceCollection collection, WebApplicationBuilder builder)
        {
            builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
            {
                var secreByte = Encoding.UTF8.GetBytes(builder.Configuration["Authentication:SecretKey"]);
                options.TokenValidationParameters = new TokenValidationParameters()
                {
                    //验证发布者
                    ValidateIssuer = true,
                    ValidIssuer = builder.Configuration["Authentication:Issuer"],
                    //验证接受者
                    ValidateAudience = true,
                    ValidAudience = builder.Configuration["Authentication:Audience"],
                    //验证是否过期
                    ValidateLifetime = true,
                    //验证私钥
                    IssuerSigningKey = new SymmetricSecurityKey(secreByte)
                };
            });
        }

    }
}
